package com.umeox.watch.moto.launcher.ui;


import static com.umeox.watch.moto.pedometer.util.ICache.KEY_DEVICE_DATA;
import static com.umeox.watch.moto.pedometer.util.ICache.KEY_LANGUAGE;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.NonNull;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.LanguageConfig;
import com.umeox.moto.common.utils.NetUtils;
import com.umeox.moto.common.utils.SystemUtils;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.pedometer.util.CacheImpl;
import com.umeox.watch.moto.pedometer.util.ICache;

public class DetectSimCardActivity extends Activity {

    private static final String TAG = DetectSimCardActivity.class.getSimpleName();

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        final ICache iCache = CacheImpl.getInstance();
        if (!iCache.get(KEY_LANGUAGE, false)) {
            SystemUtils.setLocalLanguage(LanguageConfig.LOCALE_IQIBLA_KID[0]);
            startActivity(new Intent(this, ChooseLanguageActivity.class));
            finish();
            return;
        }
        setContentView(R.layout.activity_detect_sim_card);

        mHandler.sendEmptyMessageDelayed(101, 3000);
    }

    private final Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {

            final boolean deviceSynced = CacheImpl.getInstance().get(KEY_DEVICE_DATA, false);
            boolean hasSimCard = NetUtils.hasSimCard();
            Logger.e(TAG,"deviceSynced=" + deviceSynced+" hasSimCard:"+hasSimCard);
            if (!deviceSynced) {
                Intent detectSignal = new Intent(mContext, DetectSignalActivity.class);
                startActivity(detectSignal);
                finish();
                return true;
            }

            boolean isActivated = DataProvider.isActivated();
            Logger.e(TAG,"isActivated=" + isActivated);
            if (!isActivated) {
                QrActivity.launch(DetectSimCardActivity.this);
                finish();
                return true;
            }

            Intent intent = new Intent(mContext, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
            return false;
        }
    });

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: " + this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
